package com.seu.project.seuquiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.mapper.CollegeContestMapMapper;
import com.seu.project.seuquiz.model.dto.collegeContestMap.CollegeContestMapQueryRequest;
import com.seu.project.seuquiz.model.entity.CollegeContestMap;
import com.seu.project.seuquiz.service.CollegeContestMapService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

/**
* @author lenovo
* @description 针对表【college_contest_map(学院-竞赛map表)】的数据库操作Service实现
* @createDate 2023-07-15 13:47:58
*/
@Service
public class CollegeContestMapServiceImpl extends ServiceImpl<CollegeContestMapMapper, CollegeContestMap>
    implements CollegeContestMapService {




    @Override
    public long collegeContestMapAdd(String collegeName, String contestId, Integer collegeStudentTotal, Integer joinTotal, Integer totalScore) {
        if (StringUtils.isAnyBlank(collegeName, contestId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学院名、竞赛Id不能为空");
        }

        if (collegeStudentTotal == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学院总人数不能为空");
        }

        if (joinTotal == null) {
            joinTotal = 0;
        }

        if (totalScore == null) {
            totalScore = 0;
        }

        synchronized (collegeName + contestId) {
            CollegeContestMap collegeContestMap = new CollegeContestMap();
            collegeContestMap.setCollegeName(collegeName);
            collegeContestMap.setContestId(contestId);
            collegeContestMap.setCollegeStudentTotal(collegeStudentTotal);
            collegeContestMap.setJoinTotal(joinTotal);
            collegeContestMap.setTotalScore(totalScore);

            boolean save = this.save(collegeContestMap);
            if(!save){
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
            }

            return collegeContestMap.getId();
        }
    }

    @Override
    public QueryWrapper<CollegeContestMap> getQueryWrapper(CollegeContestMapQueryRequest request) {
        QueryWrapper<CollegeContestMap> queryWrapper = new QueryWrapper<>();
        if (request == null) {
            return null;
        }

        Long id = request.getId();
        String collegeName = request.getCollegeName();
        String contestId = request.getContestId();
        Integer collegeStudentTotal = request.getCollegeStudentTotal();
        Integer joinTotal = request.getJoinTotal();
        Integer totalScore = request.getTotalScore();
//        Date createTime = request.getCreateTime();
//        Date updateTime = request.getUpdateTime();

        queryWrapper.eq(id != null && id > 0,"id", id);
        queryWrapper.like(Strings.isNotBlank(collegeName), "collegeName", "%" + collegeName + "%");
        queryWrapper.eq(Strings.isNotBlank(contestId),"contestId", contestId);
        queryWrapper.eq(collegeStudentTotal != null && collegeStudentTotal > 0, "collegeStudentTotal", collegeStudentTotal);
        queryWrapper.eq(joinTotal != null && joinTotal > 0, "joinTotal", joinTotal);
        queryWrapper.eq(totalScore != null && totalScore > 0, "totalScore", totalScore);

        return queryWrapper;
    }
}




